package com.game.commons.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;


/**
 * Properties配置文件读取
 * @author 杜祥
 * 需要外部包
 * 	commons-io-2.0.1.jar
 * 	log4j-1.2.16.jar
 */
public class PropertiesUtils 
{
	private static  Logger log = Logger.getLogger(PropertiesUtils.class);
	
	/**
	 * 写入Properties文件,通过filename
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static void write(String filename,Properties p) throws IOException
	{
		write(new File(filename), p);
	}
	
	
	/**
	 * 写入Properties文件,通过File
	 * @param file		
	 * @return
	 * @throws IOException
	 */
	public static void write(File file,Properties p) throws IOException
	{
		log.info("write: " + file);
		FileOutputStream fos = new FileOutputStream(file);
		p.store(fos, "");
		fos.close();
	}
	
	/**
	 * 读取Properties文件,通过filename
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static Properties load(String filename) throws IOException
	{
		return load(new File(filename));
	}
	
	/**
	 * 读取Properties文件,通过File
	 * @param file		
	 * @return
	 * @throws IOException
	 */
	public static Properties load(File file) throws IOException
	{
		log.info("loading: " + file);

		FileInputStream fis = new FileInputStream(file);
		Properties p = new Properties();
		p.load(fis);
		fis.close();
		return p;
	}
	
	/**
	 * 读取多个Properties文件,通过filename[]
	 * @param fileNames
	 * @return
	 * @throws IOException
	 */
	public static Properties[] loads(String... fileNames) throws IOException
	{
		Properties[] properties = new Properties[fileNames.length];
		for(int i = 0 ; i < properties.length ; i++)
		{
			properties[i] = load(fileNames[i]);
		}
		return properties;
	}
	
	/**
	 * 读取多个Properties文件,通过file[]
	 * @param files
	 * @return
	 * @throws IOException
	 */
	public static Properties[] loads(File... files) throws IOException
	{
		Properties[] properties = new Properties[files.length];
		for(int i = 0 ; i < properties.length ; i++)
		{
			properties[i] = load(files[i]);
		}
		return properties;
	}
	
	
	/**
	 * 读取某个路劲下的所有Properties文件,通过filename
	 * @param dir
	 * @return
	 * @throws IOException
	 */
	public static Properties[] loadAllFromDirectory(String dir) throws IOException
	{
		return loadAllFromDirectory(new File(dir), false);
	}
	
	/**
	 * 读取某个路劲下的所有Properties文件,通过File
	 * @param dir
	 * @return
	 * @throws IOException
	 */
	public static Properties[] loadAllFromDirectory(File dir) throws IOException
	{
		return loadAllFromDirectory(dir, false);
	}
	
	/**
	 * 读取某个路劲下的所有Properties文件,通过filename
	 * @param dir
	 * @param recursive			是否解析子目录
	 * @return
	 * @throws IOException
	 */
	public static Properties[] loadAllFromDirectory(String dir, boolean recursive) throws IOException
	{
		return loadAllFromDirectory(new File(dir), recursive);
	}
	
	/**
	 * 读取某个路劲下的所有Properties文件,通过File
	 * @param file
	 * @param recursive			是否解析子目录
	 * @return
	 * @throws IOException
	 */
	public static Properties[] loadAllFromDirectory(File file,boolean recursive) throws IOException
	{
		Collection<File> fileList = FileUtils.listFiles(file, new String[]{"properties"}, recursive);
		
		return loads(fileList.toArray(new File[fileList.size()]));
	}
}
